package jpanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.Iterator;
import java.util.Queue;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ClickTester extends JPanel {
	
	private static final long CLICK_DURATION = 10000;
	
	//keeps track of all clicks currently being drawn
	private Queue<MouseEvent> daClicks;
	private MouseEvent currentClick;
	
	//variables for drawing spirals
	private int initialAngle;
	private int angleIncrement;
	private int initialLength;
	private int lengthIncrement;
	//determines how frequently to change color within a straight line.
	private int lineStep;
	
	private class GenesisMouseAdapter extends MouseAdapter {
    		public void mouseDragged(MouseEvent e) {
    		currentClick = e;
    		repaint();
        	}
        	
        	public void mouseClicked(MouseEvent e) {
        		currentClick = e;
        		//daClicks.add(e);
        		repaint();
        	}
        	
        	public void mouseReleased(MouseEvent e) {
        		daClicks.add(e);
        		repaint();
//        		System.out.println("Click Added: \n" + daClicks.size() + " clicks in collection");
            	}
    	}
	
	public ClickTester() {
		
		daClicks = new ArrayDeque<MouseEvent>();
		setPreferredSize(new Dimension(800,600)); 
		addMouseListener(new GenesisMouseAdapter());
		addMouseMotionListener(new GenesisMouseAdapter());
    };
    
    
    public void paintComponent(final Graphics the_graphics) 
    {
          super.paintComponent(the_graphics);
          final Graphics2D g2d = (Graphics2D) the_graphics;
          setBackground(Color.BLACK);
          for(MouseEvent click:daClicks) {
              drawSpiral(click, g2d);
          }
    }

    private void drawSpiral(MouseEvent click, Graphics2D g2d) {
		int x = click.getX();
		int y = click.getY();
		//g2d.draw3DRect(x, y, 10, 10, true);
	}


	// 
    public void tick() {
//	System.out.println("Tick");
    	for(MouseEvent click:daClicks) {
    		if (click.getWhen() + CLICK_DURATION < System.currentTimeMillis()) {
    			daClicks.remove(click);
    			System.out.println("Removing Click");
    		}
    	}
    	repaint();
    }
    
}
